package top.tervest.easyshare.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.tervest.easyshare.common.Result;
import top.tervest.easyshare.domain.param.LoginParam;
import top.tervest.easyshare.domain.param.RegisterParam;
import top.tervest.easyshare.service.UserService;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ZhangNanNan
 * @create 2019-07-17 11:15
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @Value("${security.token.name}")
    private String tokenName;

    @RequestMapping("/login")
    public Result login(HttpServletResponse  response,LoginParam param){
        String token = userService.login(param);
        response.setHeader(tokenName, token);
        return Result.success(token);
    }

    @RequestMapping("/register")
    public Result register(RegisterParam param){
        String register = userService.register(param);
        return Result.success(register);
    }

    @PreAuthorize("hasRole('SUPER_ADMIN')")
    @RequestMapping("/info")
    public Result info(RegisterParam param){
        return Result.success();
    }

}
